<?php

/**
 * 管理中心仓库管理
 * $Author: 王海龙 $
 * $Id: lib_warehouse.php 17217 2014-10-10 12:05:22 王海龙 $
 */

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
require_once(ROOT_PATH . 'includes/lib_goods.php');
include_once('./includes/lib_warehouse.php');


//订单列表
if ($_REQUEST['act'] == 'order_list'){
    /* 检查权限 */
    admin_priv('order_ss_edit');

    /* 模板赋值 */
    $smarty->assign('ur_here', $_LANG['02_order_list']);
    $smarty->assign('action_link', array('href' => 'order.php?act=order_query', 'text' => $_LANG['03_order_query']));
    $smarty->assign('status_list', $_LANG['cs']);   // 订单状态
    $smarty->assign('os_unconfirmed',   OS_UNCONFIRMED);
    $smarty->assign('cs_await_pay',     CS_AWAIT_PAY);
    $smarty->assign('cs_await_ship',    CS_AWAIT_SHIP);
    $smarty->assign('full_page',        1);
    //待发货列表
    if($_REQUEST['status']=='deliver' ){
        $_REQUEST['composite_status']=101;
    }
    elseif($_REQUEST['status']=='delivered' ){
        $_REQUEST['composite_status']=102;
    }
    if ($_REQUEST['status']=='daodankuaidi') {

         $_REQUEST['daodankuaidi']=102;
        # code...
    }
    $order_list = order_list();
    $smarty->assign('order_list',   $order_list['orders']);
    $smarty->assign('filter',       $order_list['filter']);
    $smarty->assign('record_count', $order_list['record_count']);
    $smarty->assign('page_count',   $order_list['page_count']);
    $smarty->assign('sort_order_time', '<img src="images/sort_desc.gif">');
    /* 显示模板 */
    assign_query_info();
    $smarty->display('ware/ware_order_list.htm');
}
//订单列表分页
elseif ($_REQUEST['act'] == 'query')
{
    /* 检查权限 */
    admin_priv('order_ss_edit');
    $order_list = order_list();//订单列表
    $smarty->assign('order_list',   $order_list['orders']);
    $smarty->assign('filter',       $order_list['filter']);
    $smarty->assign('record_count', $order_list['record_count']);
    $smarty->assign('page_count',   $order_list['page_count']);
    $sort_flag  = sort_flag($order_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);
    make_json_result($smarty->fetch('ware/ware_order_list.htm'), '', array('filter' => $order_list['filter'], 'page_count' => $order_list['page_count']));
}
//订单详情
elseif ($_REQUEST['act'] == 'order_info' || $_REQUEST['act'] == 'info' ) {

        /* 根据订单id或订单号查询订单信息 */
    if (isset($_REQUEST['order_id']))
    {
        $order_id = intval($_REQUEST['order_id']);
        $order = order_info($order_id);
    }
    elseif (isset($_REQUEST['order_sn']))
    {
        $order_sn = trim($_REQUEST['order_sn']);
        $order = order_info(0, $order_sn);
    }
    else
    {
        /* 如果参数不存在，退出 */
        die('invalid parameter');
    }

    /* 如果订单不存在，退出 */
    if (empty($order))
    {
        die('order does not exist');
    }

    /* 根据订单是否完成检查权限 */
    if (order_finished($order))
    {
        admin_priv('order_ss_edit');
    }
    else
    {
        admin_priv('order_ss_edit');
    }

    /* 如果管理员属于某个办事处，检查该订单是否也属于这个办事处 */
    $sql = "SELECT agency_id FROM " . $ecs->table('admin_user') . " WHERE user_id = '$_SESSION[admin_id]'";
    $agency_id = $db->getOne($sql);
    if ($agency_id > 0)
    {
        if ($order['agency_id'] != $agency_id)
        {
            sys_msg($_LANG['priv_error']);
        }
    }

    /* 取得用户名 */
    if ($order['user_id'] > 0)
    {
        $user = user_info($order['user_id']);
        if (!empty($user))
        {
            $order['user_name'] = $user['user_name'];
        }
    }

    /* 取得区域名 */
    $sql = "SELECT concat(IFNULL(c.region_name, ''), '  ', IFNULL(p.region_name, ''), " .
                "'  ', IFNULL(t.region_name, ''), '  ', IFNULL(d.region_name, '')) AS region " .
            "FROM " . $ecs->table('order_info') . " AS o " .
                "LEFT JOIN " . $ecs->table('region') . " AS c ON o.country = c.region_id " .
                "LEFT JOIN " . $ecs->table('region') . " AS p ON o.province = p.region_id " .
                "LEFT JOIN " . $ecs->table('region') . " AS t ON o.city = t.region_id " .
                "LEFT JOIN " . $ecs->table('region') . " AS d ON o.district = d.region_id " .
            "WHERE o.order_id = '$order[order_id]'";
    $order['region'] = $db->getOne($sql);

    /* 格式化金额 */
    if ($order['order_amount'] < 0)
    {
        $order['money_refund']          = abs($order['order_amount']);
        $order['formated_money_refund'] = price_format(abs($order['order_amount']));
    }

    /* 其他处理 */
    $order['order_time']    = local_date($_CFG['time_format'], $order['add_time']);
    $order['pay_time']      = $order['pay_time'] > 0 ?
        local_date($_CFG['time_format'], $order['pay_time']) : $_LANG['ps'][PS_UNPAYED];
    $order['shipping_time'] = $order['shipping_time'] > 0 ?
        local_date($_CFG['time_format'], $order['shipping_time']) : $_LANG['ss'][SS_UNSHIPPED];
    $order['status']        = $_LANG['os'][$order['order_status']] . ',' . $_LANG['ps'][$order['pay_status']] . ',' . $_LANG['ss'][$order['shipping_status']];
    $order['invoice_no']    = $order['shipping_status'] == SS_UNSHIPPED || $order['shipping_status'] == SS_PREPARING ? $_LANG['ss'][SS_UNSHIPPED] : $order['invoice_no'];

    /* 取得订单的来源 */
    if ($order['from_ad'] == 0)
    {
        $order['referer'] = empty($order['referer']) ? $_LANG['from_self_site'] : $order['referer'];
    }
    elseif ($order['from_ad'] == -1)
    {
        $order['referer'] = $_LANG['from_goods_js'] . ' ('.$_LANG['from'] . $order['referer'].')';
    }
    else
    {
        /* 查询广告的名称 */
         $ad_name = $db->getOne("SELECT ad_name FROM " .$ecs->table('ad'). " WHERE ad_id='$order[from_ad]'");
         $order['referer'] = $_LANG['from_ad_js'] . $ad_name . ' ('.$_LANG['from'] . $order['referer'].')';
    }

    /* 取得订单商品总重量 */
    $weight_price = order_weight_price($order['order_id']);
    $order['total_weight'] = $weight_price['formated_weight'];

    /* 参数赋值：订单 */
    $smarty->assign('order', $order);

    /* 取得用户信息 */
    if ($order['user_id'] > 0)
    {
        /* 用户等级 */
        if ($user['user_rank'] > 0)
        {
            $where = " WHERE rank_id = '$user[user_rank]' ";
        }
        else
        {
            $where = " WHERE min_points <= " . intval($user['rank_points']) . " ORDER BY min_points DESC ";
        }
        $sql = "SELECT rank_name FROM " . $ecs->table('user_rank') . $where;
        $user['rank_name'] = $db->getOne($sql);

        $smarty->assign('user', $user);

        // 地址信息
        $sql = "SELECT * FROM " . $ecs->table('user_address') . " WHERE user_id = '$order[user_id]'";
        $smarty->assign('address_list', $db->getAll($sql));
    }

    /* 取得订单商品及货品 */
    $goods_list = array();
    $goods_attr = array();
    $sql = "SELECT o.*, o.goods_attr, IFNULL(b.brand_name, '') AS brand_name
            FROM " . $ecs->table('order_goods') . " AS o
                LEFT JOIN " . $ecs->table('goods') . " AS g
                    ON o.goods_id = g.goods_id
                LEFT JOIN " . $ecs->table('brand') . " AS b
                    ON g.brand_id = b.brand_id
            WHERE o.order_id = '$order[order_id]'";
    $res = $db->query($sql);
    while ($row = $db->fetchRow($res))
    {

        $row['formated_subtotal']       = price_format($row['goods_price'] * $row['goods_number']);
        $row['formated_goods_price']    = price_format($row['goods_price']);

        $goods_attr[] = explode(' ', trim($row['goods_attr'])); //将商品属性拆分为一个数组

        if ($row['extension_code'] == 'package_buy')
        {
            $row['storage'] = '';
            $row['brand_name'] = '';
            $row['package_goods_list'] = get_package_goods($row['goods_id']);
        }

        $goods_list[] = $row;
    }

    $attr = array();
    $arr  = array();
    foreach ($goods_attr AS $index => $array_val)
    {
        foreach ($array_val AS $value)
        {
            $arr = explode(':', $value);//以 : 号将属性拆开
            $attr[$index][] =  @array('name' => $arr[0], 'value' => $arr[1]);
        }
    }

    $smarty->assign('goods_attr', $attr);
    $smarty->assign('goods_list', $goods_list);


    /* 取得订单操作记录 */
    $act_list = array();
    $sql = "SELECT * FROM " . $ecs->table('order_action') . " WHERE order_id = '$order[order_id]' ORDER BY log_time DESC,action_id DESC";
    $res = $db->query($sql);
    while ($row = $db->fetchRow($res))
    {
        $row['order_status']    = $_LANG['os'][$row['order_status']];
        $row['pay_status']      = $_LANG['ps'][$row['pay_status']];
        $row['shipping_status'] = $_LANG['ss'][$row['shipping_status']];
        $row['action_time']     = local_date($_CFG['time_format'], $row['log_time']);
        $act_list[] = $row;
    }
    $smarty->assign('action_list', $act_list);

    /* 取得是否存在实体商品 */
    $smarty->assign('exist_real_goods', exist_real_goods($order['order_id']));

    /* 是否打印订单，分别赋值 */
    if (isset($_GET['print']))
    {
        $smarty->assign('shop_name',    $_CFG['shop_name']);
        $smarty->assign('shop_url',     $ecs->url());
        $smarty->assign('shop_address', $_CFG['shop_address']);
        $smarty->assign('service_phone',$_CFG['service_phone']);
        $smarty->assign('print_time',   local_date($_CFG['time_format']));
        $smarty->assign('action_user',  $_SESSION['admin_name']);

        $smarty->display('order_print.htm');
    }
    /* 打印快递单 */
    elseif (isset($_GET['shipping_print']))
    {
        //$smarty->assign('print_time',   local_date($_CFG['time_format']));
        //发货地址所在地
        $region_array = array();
        $region_id = !empty($_CFG['shop_country']) ? $_CFG['shop_country'] . ',' : '';
        $region_id .= !empty($_CFG['shop_province']) ? $_CFG['shop_province'] . ',' : '';
        $region_id .= !empty($_CFG['shop_city']) ? $_CFG['shop_city'] . ',' : '';
        $region_id .= $order['country'].','.$order['province'].','.$order['city'].','.$order['district'].',';
        $region_id = substr($region_id, 0, -1);
        $region = $db->getAll("SELECT region_id, region_name FROM " . $ecs->table("region") . " WHERE region_id IN ($region_id)");
        if (!empty($region))
        {
            foreach($region as $region_data)
            {
                $region_array[$region_data['region_id']] = $region_data['region_name'];
            }
        }
        $smarty->assign('shop_name',    $_CFG['shop_name']);
        $smarty->assign('order_id',    $order_id);
        $smarty->assign('province', $region_array[$_CFG['shop_province']]);
        $smarty->assign('city', $region_array[$_CFG['shop_city']]);
        $smarty->assign('shop_address', $_CFG['shop_address']);
        $smarty->assign('service_phone',$_CFG['service_phone']);
        $shipping = $db->getRow("SELECT * FROM " . $ecs->table("shipping") . " WHERE shipping_id = " . $order['shipping_id']);
        /* 取快递单背景宽高 */
        $shipping['print_bg_size'] = array('width' => '800', 'height' => '467');
        /* 设置打印模板 */
        if($order['agency_id'] == 1 && $order['pay_id'] != 3) $bg_type = 1; // 广东省在线支付方式
        elseif($order['agency_id'] == 1 && $order['pay_id'] == 3) $bg_type = 2; // 广东省到付方式
        elseif($order['pay_id'] != 3) $bg_type = 3;// 杭州仓在线支付方式
        elseif($order['pay_id'] == 3) $bg_type = 4;// 杭州仓到付方式
        if($bg_type == 1){
            $shipping['print_bg']='http://www.eggms.com/images/receipt/dly_kaidi_2.jpg';
            $shipping['config_lable'] = "t_shop_province,网店-省份,63,21,131,161,b_shop_province||,||t_customer_province,收件人-省份,63,21,498,158,b_customer_province||,||t_shop_address,网店-地址,184,21,130,207,b_shop_address||,||t_shop_name,网店-名称,180,21,134,87,b_shop_name||,||t_shop_tel,网店-联系电话,133,21,179,236,b_shop_tel||,||t_customer_tel,收件人-电话,135,21,648,240,b_customer_tel||,||t_customer_mobel,收件人-手机,134,21,600,240,b_customer_mobel||,||t_customer_name,收件人-姓名,91,21,506,92,b_customer_name||,||t_customer_address,收件人-详细地址,318,21,461,195,b_customer_address||,||t_months,月-当日日期,39,21,200,385,b_months||,||t_day,日-当日日期,28,21,238,385,b_day||,||t_shop_city,网店-城市,65,21,215,161,b_shop_city||,||t_shop_district,网店-区/县,68,21,304,158,b_shop_district||,||t_customer_city,收件人-城市,63,21,590,157,b_customer_city||,||t_customer_district,收件人-区/县,70,21,672,157,b_customer_district||,||t_shipping_fee,邮费已收,134,21,690,310,t_shipping_fee||,||t_year,年-当日日期,47,21,145,385,b_year||,||";
        }elseif($bg_type == 2){
            $shipping['print_bg']='http://www.eggms.com/images/receipt/dly_kaidi_1.jpg';
            $shipping['config_lable'] = "t_shop_province,网店-省份,63,21,129,192,b_shop_province||,||t_customer_province,收件人-省份,63,21,422,188,b_customer_province||,||t_shop_address,网店-地址,184,21,128,231,b_shop_address||,||t_shop_name,网店-名称,180,21,183,141,b_shop_name||,||t_shop_tel,网店-联系电话,133,21,248,110,b_shop_tel||,||t_customer_tel,收件人-电话,135,21,573,135,b_customer_tel||,||t_customer_mobel,收件人-手机,134,21,573,93,b_customer_mobel||,||t_customer_name,收件人-姓名,91,21,423,93,b_customer_name||,||t_customer_address,收件人-详细地址,318,21,394,220,b_customer_address||,||t_months,月-当日日期,27,21,359,316,b_months||,||t_day,日-当日日期,28,21,391,316,b_day||,||t_shop_city,网店-城市,65,21,197,192,b_shop_city||,||t_shop_district,网店-区/县,68,21,273,191,b_shop_district||,||t_customer_city,收件人-城市,63,21,500,186,b_customer_city||,||t_customer_district,收件人-区/县,96,21,573,186,b_customer_district||,||t_pigeon,√-对号,21,21,320,373,b_pigeon||,||t_total_fee,小写金额,99,21,742,359,t_total_fee||,||t_shipping_fee,邮费已收,99,21,728,400,t_shipping_fee||,||t_year,年-当日日期,47,21,308,316,b_year||,||";
        }elseif($bg_type == 3){
            $shipping['print_bg']='http://www.eggms.com/images/receipt/dly_kaidi_2.jpg';
            $shipping['config_lable'] = "t_shop_province,网店-省份,63,21,131,161,b_shop_province||,||t_customer_province,收件人-省份,63,21,498,158,b_customer_province||,||t_shop_address,网店-地址,184,21,130,207,b_shop_address||,||t_shop_name,网店-名称,180,21,134,87,b_shop_name||,||t_shop_tel,网店-联系电话,133,21,179,236,b_shop_tel||,||t_customer_tel,收件人-电话,135,21,648,231,b_customer_tel||,||t_customer_mobel,收件人-手机,134,21,600,231,b_customer_mobel||,||t_customer_name,收件人-姓名,91,21,506,92,b_customer_name||,||t_customer_address,收件人-详细地址,318,21,461,195,b_customer_address||,||t_months,月-当日日期,39,21,200,385,b_months||,||t_day,日-当日日期,28,21,238,385,b_day||,||t_shop_city,网店-城市,65,21,215,161,b_shop_city||,||t_shop_district,网店-区/县,68,21,304,158,b_shop_district||,||t_customer_city,收件人-城市,63,21,590,157,b_customer_city||,||t_customer_district,收件人-区/县,70,21,672,157,b_customer_district||,||t_shipping_fee,邮费已收,134,21,690,310,t_shipping_fee||,||t_year,年-当日日期,47,21,145,385,b_year||,||";
        }elseif($bg_type == 4){
            $shipping['print_bg']='/Public/images/kaidi_3.jpg';
            $shipping['config_lable'] = "t_shop_province,网店-省份,63,21,161,141,b_shop_province||,||t_customer_province,收件人-省份,63,21,478,128,b_customer_province||,||t_shop_address,网店-地址,184,21,160,187,b_shop_address||,||t_shop_name,网店-名称,180,21,134,87,b_shop_name||,||t_shop_tel,网店-联系电话,133,21,179,200,b_shop_tel||,||t_customer_tel,收件人-电话,135,21,648,200,b_customer_tel||,||t_customer_mobel,收件人-手机,134,21,600,200,b_customer_mobel||,||t_customer_name,收件人-姓名,91,21,506,62,b_customer_name||,||t_customer_address,收件人-详细地址,318,21,426,165,b_customer_address||,||t_months,月-当日日期,39,21,180,385,b_months||,||t_day,日-当日日期,28,21,218,385,b_day||,||t_shop_city,网店-城市,65,21,245,141,b_shop_city||,||t_shop_district,网店-区/县,68,21,334,138,b_shop_district||,||t_customer_city,收件人-城市,63,21,570,127,b_customer_city||,||t_customer_district,收件人-区/县,70,21,652,127,b_customer_district||,||t_pigeon,√-对号,21,21,530,223,b_pigeon||,||t_total_fee,小写金额,99,21,470,335,t_total_fee||,||t_shipping_fee,邮费已收,99,21,680,354,t_shipping_fee||,||t_year,年-当日日期,47,21,125,385,b_year||,||";
        }elseif($bg_type == 5){
            $shipping['print_bg']='http://www.eggms.com/images/receipt/dly_kaidi_2.jpg';
            $shipping['config_lable'] = "t_shop_province,网店-省份,63,21,131,161,b_shop_province||,||t_customer_province,收件人-省份,63,21,498,158,b_customer_province||,||t_shop_address,网店-地址,184,21,130,207,b_shop_address||,||t_shop_name,网店-名称,180,21,134,87,b_shop_name||,||t_shop_tel,网店-联系电话,133,21,179,236,b_shop_tel||,||t_customer_tel,收件人-电话,135,21,648,240,b_customer_tel||,||t_customer_mobel,收件人-手机,134,21,600,240,b_customer_mobel||,||t_customer_name,收件人-姓名,91,21,506,92,b_customer_name||,||t_customer_address,收件人-详细地址,318,21,461,195,b_customer_address||,||t_months,月-当日日期,39,21,200,385,b_months||,||t_day,日-当日日期,28,21,238,385,b_day||,||t_shop_city,网店-城市,65,21,215,161,b_shop_city||,||t_shop_district,网店-区/县,68,21,304,158,b_shop_district||,||t_customer_city,收件人-城市,63,21,590,157,b_customer_city||,||t_customer_district,收件人-区/县,70,21,672,157,b_customer_district||,||t_shipping_fee,邮费已收,134,21,690,310,t_shipping_fee||,||t_year,年-当日日期,47,21,145,385,b_year||,||";
        }elseif($bg_type == 6){
            $shipping['print_bg']='/Public/images/kaidi_2.jpg';
            $shipping['config_lable'] = "t_shop_province,网店-省份,63,21,129,192,b_shop_province||,||t_customer_province,收件人-省份,63,21,422,188,b_customer_province||,||t_shop_address,网店-地址,184,21,128,231,b_shop_address||,||t_shop_name,网店-名称,180,21,183,141,b_shop_name||,||t_shop_tel,网店-联系电话,133,21,248,110,b_shop_tel||,||t_customer_tel,收件人-电话,135,21,573,135,b_customer_tel||,||t_customer_mobel,收件人-手机,134,21,573,103,b_customer_mobel||,||t_customer_name,收件人-姓名,91,21,423,112,b_customer_name||,||t_customer_address,收件人-详细地址,318,21,394,220,b_customer_address||,||t_months,月-当日日期,27,21,359,316,b_months||,||t_day,日-当日日期,28,21,391,316,b_day||,||t_shop_city,网店-城市,65,21,197,192,b_shop_city||,||t_shop_district,网店-区/县,68,21,273,191,b_shop_district||,||t_customer_city,收件人-城市,63,21,500,186,b_customer_city||,||t_customer_district,收件人-区/县,96,21,573,186,b_customer_district||,||t_pigeon,√-对号,21,21,520,383,b_pigeon||,||t_total_fee,小写金额,99,21,732,349,t_total_fee||,||t_shipping_fee,邮费已收,99,21,728,384,t_shipping_fee||,||t_year,年-当日日期,47,21,308,316,b_year||,||";
        }
        //打印单模式
        if ($shipping['print_model'] == 2)
        {

            /* 取快递单背景宽高 */
            $shipping['print_bg_size'] = array('width' => '800', 'height' => '467');
            /* 标签信息 */
            $lable_box = array();
            $lable_box['t_shop_country'] = $region_array[$_CFG['shop_country']]; //网店-国家
            $lable_box['t_shop_city'] = $region_array[$_CFG['shop_city']]; //网店-城市
            $lable_box['t_shop_province'] = $region_array[$_CFG['shop_province']]; //网店-省份
            $lable_box['t_shop_name'] = $_CFG['shop_name']; //网店-名称
            $lable_box['t_shop_district'] = ''; //网店-区/县
            $lable_box['t_shop_tel'] = $_CFG['service_phone']; //网店-联系电话
            $lable_box['t_shop_address'] = $_CFG['shop_address']; //网店-地址
            $lable_box['t_customer_country'] = $region_array[$order['country']]; //收件人-国家
            $lable_box['t_customer_province'] = $region_array[$order['province']]; //收件人-省份
            $lable_box['t_customer_city'] = $region_array[$order['city']]; //收件人-城市
            $lable_box['t_customer_district'] = $region_array[$order['district']]; //收件人-区/县
            $lable_box['t_customer_tel'] = $order['tel']; //收件人-电话
            $lable_box['t_customer_mobel'] = $order['mobile']; //收件人-手机
            $lable_box['t_customer_post'] = $order['zipcode']; //收件人-邮编
            $lable_box['t_customer_address'] = $order['address']; //收件人-详细地址
            $lable_box['t_customer_name'] = $order['consignee']; //收件人-姓名
            $lable_box['t_total_fee_cny'] = cny(round($order['order_amounts'],1)); //代收费用
            $lable_box['t_total_fee'] = $order['order_amounts']; //代收费用
            $gmtime_utc_temp = gmtime(); //获取 UTC 时间戳
            $lable_box['t_year'] = date('Y', $gmtime_utc_temp); //年-当日日期
            $lable_box['t_months'] = date('m', $gmtime_utc_temp); //月-当日日期
            $lable_box['t_day'] = date('d', $gmtime_utc_temp); //日-当日日期

            $lable_box['t_order_no'] = $order['order_sn']; //订单号-订单
            $lable_box['t_order_postscript'] = $order['postscript']; //备注-订单
            $lable_box['t_order_best_time'] = $order['best_time']; //送货时间-订单
            $lable_box['t_pigeon'] = '√'; //√-对号
            $lable_box['t_custom_content'] = ''; //自定义内容

            //标签替换
            $temp_config_lable = explode('||,||', $shipping['config_lable']);
            if (!is_array($temp_config_lable))
            {
                $temp_config_lable[] = $shipping['config_lable'];
            }
            foreach ($temp_config_lable as $temp_key => $temp_lable)
            {
                $temp_info = explode(',', $temp_lable);
                if (is_array($temp_info))
                {
                    $temp_info[1] = $lable_box[$temp_info[0]];
                }
                $temp_config_lable[$temp_key] = implode(',', $temp_info);
            }
            $shipping['config_lable'] = implode('||,||',  $temp_config_lable);

            $smarty->assign('shipping', $shipping);

            $smarty->display('print.htm');
        }
        elseif (!empty($shipping['shipping_print']))
        {
            /* 代码 */
            echo $smarty->fetch("str:" . $shipping['shipping_print']);
        }
        else
        {
            $shipping_code = $db->getOne("SELECT shipping_code FROM " . $ecs->table('shipping') . " WHERE shipping_id=" . $order['shipping_id']);
            if ($shipping_code)
            {
                include_once(ROOT_PATH . 'includes/modules/shipping/' . $shipping_code . '.php');
            }

            if (!empty($_LANG['shipping_print']))
            {
                echo $smarty->fetch("str:$_LANG[shipping_print]");
            }
            else
            {
                echo $_LANG['no_print_shipping'];
            }
        }
    }
    else
    {
        /* 模板赋值 */
        $smarty->assign('ur_here', $_LANG['order_info']);
        $smarty->assign('action_link', array('href' => 'order.php?act=list&' . list_link_postfix(), 'text' => $_LANG['02_order_list']));

        /* 显示模板 */
        assign_query_info();
        $smarty->assign('order_id', $order_id);
        $smarty->display('ware/ware_order_info.htm');
    }
}
//仓库操作订单操作，发货 ，取消发货
elseif ($_REQUEST['act'] == 'ware_operate') {
        $invoice_no=!empty($_REQUEST['invoice_no'])?$_REQUEST['invoice_no']:'';
        $order_id=!empty($_REQUEST['order_id'])?$_REQUEST['order_id']:'';
        $note=!empty($_REQUEST['note'])?$_REQUEST['note']:'';
        $return['error']='1';
        $return['message']="";

        $order_info=order_info($order_id);
        //订单发货
        if($_REQUEST['step']=='deliver_goods'){
            if(empty($invoice_no)){
                $return['error']='0';
                $return['message']='请输入发货单号再发货';
                echo json_encode($return);
                exit();
            }
            //根据订单状态判断能否发货
            if($order_info['order_status']>2||$order_info['shipping_status']>0||($order_info['pay_id']==1&&$order_info['pay_status']!=2)){
                $return['error']='0';
                $return['message']='该订单不能发货请联系客服';
                echo json_encode($return);
                exit(); 
            }
            else{
                //更新发货单号与订单状态
                $arr['shipping_status'] = SS_SHIPPED;
                $arr['shipping_time'] = time();
                $arr['invoice_no'] = trim($invoice_no);
                $arr['order_status'] = OS_SPLITED;
                update_order($order_id, $arr);

                $return['message']='发货成功';
                /* 记录log */
                order_action($order_info['order_sn'], $order_info['order_status'], '1', $order_info['pay_status'], '仓库发货');
                //记录管理员操作
                ware_log($_SESSION['admin_name'],'1','正常发货',$area_id);
                echo json_encode($return);
                exit(); 
            }
        }
        //取消发货
        elseif ($_REQUEST['step']=='cancel_goods') {
            //更新发货单号与订单状态
            $arr['shipping_status'] = 0;
            $arr['shipping_time']   = '';
            $arr['invoice_no']      = '';
            $arr['order_status']    = 1;
            update_order($order_id, $arr);

            $return['message'] = '取消发货成功';
            /* 记录log */
            order_action($order_info['order_sn'], $order_info['order_status'], '0', $order_info['pay_status'], $note);
            //记录管理员操作
            ware_log($_SESSION['admin_name'], '2', $note,$area_id);
            echo json_encode($return);
            exit(); 
           
        }
        //修改发货单号
        elseif ($_REQUEST['step']=='edit_invoice_no') {
            //更新发货单号与订单状态
                $sql="UPDATE " . $GLOBALS['ecs']->table('order_info').
                 " set invoice_no=$invoice_no" . 
                 " WHERE order_id=$order_id";
                 $db->query($sql);
                $return['message'] = '修改发货单号成功';
                /* 记录log */
                order_action($order_info['order_sn'], $order_info['order_status'], $order_info['shipping_status'], $order_info['pay_status'], $note);
                //记录管理员操作
                ware_log($_SESSION['admin_name'], '4', $note, $area_id);
                echo json_encode($return);
                exit(); 
        }




}
elseif ($_REQUEST['act'] == 'goods_list') {
    /* 检查权限 */
    admin_priv('ware_goods_stock');
    /* 模板赋值 */
    $goods_list = goods_list();//商品列表
    $smarty->assign('cat_list',     cat_list(0, $cat_id)); // 下拉分类列表
    $smarty->assign('agency_list',  agency_list());//办事处列表
    $smarty->assign('goods_list',   $goods_list['goods']);
    $smarty->assign('filter',       $goods_list['filter']);
    $smarty->assign('record_count', $goods_list['record_count']);
    $smarty->assign('page_count',   $goods_list['page_count']);
    $smarty->assign('full_page',    1);
    /* 排序标记 */
    $sort_flag  = sort_flag($goods_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);
    /* 显示商品列表页面 */
    assign_query_info();
    $htm_file = ($_REQUEST['act'] == 'list') ?
        'ware/ware_goods_list.htm' : (($_REQUEST['act'] == 'trash') ? 'goods_trash.htm' : 'group_list.htm');
    $smarty->display('ware/ware_goods_list.htm');
}
//商品分页
elseif ($_REQUEST['act'] == 'goods_query')
{
    $is_delete = empty($_REQUEST['is_delete']) ? 0 : intval($_REQUEST['is_delete']);
    $goods_list = goods_list($is_delete);
    $smarty->assign('code',         $code);
    $smarty->assign('goods_list',   $goods_list['goods']);
    $smarty->assign('filter',       $goods_list['filter']);
    $smarty->assign('record_count', $goods_list['record_count']);
    $smarty->assign('page_count',   $goods_list['page_count']);
    $smarty->assign('list_type',    $is_delete ? 'trash' : 'goods');
    $smarty->assign('use_storage',  empty($_CFG['use_storage']) ? 0 : 1);
    make_json_result($smarty->fetch('ware/ware_goods_list.htm'), '',
    array('filter' => $goods_list['filter'], 'page_count' => $goods_list['page_count']));
}
//进货单列表
elseif ($_REQUEST['act'] == 'buy_list'){
    /* 检查权限 */
    admin_priv('buy_info');
    // /* 模板赋值 */
    $smarty->assign('ur_here', "进货单列表");
    $smarty->assign('action_link', array('href' => 'warehouse.php?act=add_buy', 'text' => "添加进货单"));
    $smarty->assign('agency_list',  agency_list());//办事处列表
    $buy_list = buy_list();//进货单列表
    $smarty->assign('filter',       $buy_list['filter']);
    $smarty->assign('record_count', $buy_list['record_count']);
    $smarty->assign('page_count',   $buy_list['page_count']);
    $smarty->assign('buy_list',   $buy_list['buy']);

    $smarty->assign('full_page',    1);
    // /* 排序标记 */
    $smarty->display('ware/buy_list.htm');
}
//进货单分页
elseif ($_REQUEST['act'] == 'buy_query')
{
    $is_delete = empty($_REQUEST['is_delete']) ? 0 : intval($_REQUEST['is_delete']);
    $buy_list = buy_list();//进货单列表
    $smarty->assign('code',         $code);
    $smarty->assign('buy_list',   $buy_list['buy']);
    $smarty->assign('filter',       $buy_list['filter']);
    $smarty->assign('record_count', $buy_list['record_count']);
    $smarty->assign('page_count',   $buy_list['page_count']);
    $smarty->assign('list_type',    $is_delete ? 'trash' : 'goods');
    $smarty->assign('use_storage',  empty($_CFG['use_storage']) ? 0 : 1);
    make_json_result($smarty->fetch('ware/buy_list.htm'), '',
    array('filter' => $buy_list['filter'], 'page_count' => $buy_list['page_count']));
}
//添加进货单
elseif ($_REQUEST['act'] == 'add_buy'){
    $smarty->assign('agency_list',  agency_list());//办事处列表
    $smarty->assign('admin_info',  admin_info()); //管理员信息
    //AJAX获取商品
    if($_REQUEST['step']=="obtain_goods"){
            $keyword=$_REQUEST['keyword'];
            $_REQUEST['page_size']=150;
            if(!empty($keyword)){
            $goods_list = goods_list();
            $count=count($goods_list['goods']);
            // 根据结果返回信息
            if($count>0){
                    $smarty->assign('goods_list',   $goods_list['goods']);
                    $return=$smarty->fetch('ware/buy_add_goods.htm');
            }
            // elseif ($count=1) {
            // }
            // else{
            // }
            echo json_encode($return);
            exit();
            }
    }
    //导入EXCEL获取商品
    elseif ($_REQUEST['step']=="inport_goods") {
            include_once('./includes/PHPExcel.php');
            $filename = $_FILES['file']['name'];
            $tmp_name = $_FILES['file']['tmp_name'];
            $objPHPExcel = PHPExcel_IOFactory::load($tmp_name);
            $indata = $objPHPExcel->getSheet(0)->toArray();
            $area_id=$indata["1"]['6'];
            foreach ($indata as $key => $value) {
                if($key>0){
                $sql =  " SELECT g.goods_id,g.goods_name,ga.attr_value,gs.goods_attr_id,gs.area_id,gs.goods_xprice,gs.goods_number,gs.goods_sku,gs.remind_num,sa.area_name ".
                        " FROM ". $GLOBALS['ecs']->table('goods_stock') ."AS gs".
                        " LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g ON g.goods_id = gs.goods_id".
                        " LEFT JOIN ".$GLOBALS['ecs']->table('goods_attr')." AS ga ON gs.goods_attr_id = ga.goods_attr_id".
                        " LEFT JOIN ".$GLOBALS['ecs']->table('service_area')." AS sa ON sa.area_id = gs.area_id ".
                        " WHERE gs.area_id = $area_id AND gs.goods_sku = $value[3] ";
                $row = $GLOBALS['db']->getRow($sql);
                $goods_list[$key]=$row;
                $goods_list[$key]['goods_number']=$value['4'];
                $goods_list[$key]['goods_price']=$value['5'];
                }
            }
            $smarty->assign('goods_list',   $goods_list);
    }
    //完成进货单
    elseif ($_REQUEST['step']=="complete") {
            $goods_id=!empty($_REQUEST['goods_id'])?$_REQUEST['goods_id']:'';
            $goods_attr_id=!empty($_REQUEST['goods_attr_id'])?$_REQUEST['goods_attr_id']:'';
            $goods_name=!empty($_REQUEST['goods_name'])?$_REQUEST['goods_name']:'';
            $goods_attr_name=!empty($_REQUEST['goods_attr_name'])?$_REQUEST['goods_attr_name']:'';
            $goods_sku=!empty($_REQUEST['goods_sku'])?$_REQUEST['goods_sku']:'';
            $goods_number=!empty($_REQUEST['goods_number'])?$_REQUEST['goods_number']:'';
            $goods_price=!empty($_REQUEST['goods_price'])?$_REQUEST['goods_price']:'';
            $buy_code=!empty($_REQUEST['buy_code'])?$_REQUEST['buy_code']:'';
            $note=!empty($_REQUEST['note'])?$_REQUEST['note']:'';
            $area_id=!empty($_REQUEST['area_id'])?$_REQUEST['area_id']:'';
            //插入入货单信息
            $buy_info = array(
                'user_name'      => $_SESSION['admin_name'],
                'add_time'   => time(),
                'buy_code'        => $buy_code,
                'area_id'           => $area_id,
                'note'           => $note,
            );
            do
            {
                $buy_info['buy_sn'] = get_order_sn();
                if ($db->autoExecute($ecs->table('ware_buy_info'), $buy_info, 'INSERT', '', 'SILENT'))
                {
                    break;
                }
                else
                {
                    if ($db->errno() != 1062)
                    {
                        die($db->error());
                    }
                }
            }
            while (true); // 防止订单号重复
            $buy_id = $db->insert_id();
            //重组商品信息
            foreach ($goods_id as $key => $value) {
                $goods_list[$key]['buy_id']=$buy_id;
                $goods_list[$key]['goods_id']=$value;
                $goods_list[$key]['goods_attr_id']=$goods_attr_id[$key];
                $goods_list[$key]['goods_name']=$goods_name[$key];
                $goods_list[$key]['goods_attr_name']=$goods_attr_name[$key];
                $goods_list[$key]['goods_sku']=$goods_sku[$key];
                $goods_list[$key]['goods_number']=$goods_number[$key];
                $goods_list[$key]['goods_price']=$goods_price[$key];
            }
            //循环插入进货商品
            foreach ($goods_list as $key => $value) {
                $db->autoExecute($ecs->table('ware_buy_goods'), $value, 'INSERT', '', 'SILENT');
                //增加库存
                change_goods_storage($value['goods_sku'],$area_id,$value['goods_number']);
            }
            //记录管理员操作
            ware_log($_SESSION['admin_name'],'4',$note,$area_id);
            //完成返回入货单列表
            header("Location: warehouse.php?act=buy_list");
    }
    $smarty->display('ware/buy_add.htm');
}
//进货单详情
elseif ($_REQUEST['act']=="buy_info") {
        /* 检查权限 */
        admin_priv('buy_info');
        /* 模板赋值 */
        $smarty->assign('ur_here', "进货单详情");
        $smarty->assign('action_link', array('href' => 'warehouse.php?act=buy_list', 'text' => "进货单列表"));
        $buy_id=!empty($_REQUEST['buy_id'])?$_REQUEST['buy_id']:'';
        if(empty($buy_id)){
            header("Location: warehouse.php?act=buy_list");
        }
        // 进货单信息
        $buy_info=buy_info($buy_id);
        $smarty->assign('buy_info',   $buy_info['info']);
        $smarty->assign('goods_list',   $buy_info['goods']);
        $smarty->display('ware/buy_info.htm'); 
}


//出货单列表
elseif ($_REQUEST['act'] == 'out_list'){
    /* 检查权限 */
    admin_priv('out_info');
    // /* 模板赋值 */
    $smarty->assign('ur_here', "出货单列表");
    $smarty->assign('action_link', array('href' => 'warehouse.php?act=add_out', 'text' => "添加出货单"));
    $smarty->assign('agency_list',  agency_list());//办事处列表
    $out_list = out_list();//进货单列表
    $smarty->assign('filter',       $out_list['filter']);
    $smarty->assign('record_count', $out_list['record_count']);
    $smarty->assign('page_count',   $out_list['page_count']);
    $smarty->assign('out_list',   $out_list['out']);

    $smarty->assign('full_page',    1);
    // /* 排序标记 */
    $smarty->display('ware/out_list.htm');
}
//出货单分页
elseif ($_REQUEST['act'] == 'out_query')
{
    $is_delete = empty($_REQUEST['is_delete']) ? 0 : intval($_REQUEST['is_delete']);
    $out_list = out_list();//进货单列表
    $smarty->assign('code',         $code);
    $smarty->assign('out_list',   $out_list['out']);
    $smarty->assign('filter',       $out_list['filter']);
    $smarty->assign('record_count', $out_list['record_count']);
    $smarty->assign('page_count',   $out_list['page_count']);
    $smarty->assign('list_type',    $is_delete ? 'trash' : 'goods');
    $smarty->assign('use_storage',  empty($_CFG['use_storage']) ? 0 : 1);
    make_json_result($smarty->fetch('ware/out_list.htm'), '',
    array('filter' => $out_list['filter'], 'page_count' => $out_list['page_count']));
}

//添加出货单
elseif ($_REQUEST['act'] == 'add_out'){
    $smarty->assign('agency_list',  agency_list());//办事处列表
    $smarty->assign('admin_info',  admin_info()); //管理员信息
    //AJAX获取商品
    if($_REQUEST['step']=="obtain_goods"){
            $keyword=$_REQUEST['keyword'];
            $_REQUEST['page_size']=150;
            if(!empty($keyword)){
            $goods_list = goods_list();
            $count=count($goods_list['goods']);
            // 根据结果返回信息
            if($count>0){
                    $smarty->assign('goods_list',   $goods_list['goods']);
                    $return=$smarty->fetch('ware/out_add_goods.htm');
            }
            // elseif ($count=1) {
            // }
            // else{
            // }
            echo json_encode($return);
            exit();
            }
    }
    //导入EXCEL获取商品
    elseif ($_REQUEST['step']=="inport_goods") {
            include_once('./includes/PHPExcel.php');
            $filename = $_FILES['file']['name'];
            $tmp_name = $_FILES['file']['tmp_name'];
            $objPHPExcel = PHPExcel_IOFactory::load($tmp_name);
            $indata = $objPHPExcel->getSheet(0)->toArray();
            $area_id=$indata["1"]['6'];
            foreach ($indata as $key => $value) {
                if($key>0){
                $sql =  " SELECT g.goods_id,g.goods_name,ga.attr_value,gs.goods_attr_id,gs.area_id,gs.goods_xprice,gs.goods_number,gs.goods_sku,gs.remind_num,sa.area_name ".
                        " FROM ". $GLOBALS['ecs']->table('goods_stock') ."AS gs".
                        " LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g ON g.goods_id = gs.goods_id".
                        " LEFT JOIN ".$GLOBALS['ecs']->table('goods_attr')." AS ga ON gs.goods_attr_id = ga.goods_attr_id".
                        " LEFT JOIN ".$GLOBALS['ecs']->table('service_area')." AS sa ON sa.area_id = gs.area_id ".
                        " WHERE gs.area_id = $area_id AND gs.goods_sku = $value[3] ";
                $row = $GLOBALS['db']->getRow($sql);
                $goods_list[$key]=$row;
                $goods_list[$key]['goods_number']=$value['4'];
                $goods_list[$key]['goods_price']=$value['5'];
                }
            }
            $smarty->assign('goods_list',   $goods_list);
    }
    //完成出货单
    elseif ($_REQUEST['step']=="complete") {
            $goods_id=!empty($_REQUEST['goods_id'])?$_REQUEST['goods_id']:'';
            $goods_attr_id=!empty($_REQUEST['goods_attr_id'])?$_REQUEST['goods_attr_id']:'';
            $goods_name=!empty($_REQUEST['goods_name'])?$_REQUEST['goods_name']:'';
            $goods_attr_name=!empty($_REQUEST['goods_attr_name'])?$_REQUEST['goods_attr_name']:'';
            $goods_sku=!empty($_REQUEST['goods_sku'])?$_REQUEST['goods_sku']:'';
            $goods_number=!empty($_REQUEST['goods_number'])?$_REQUEST['goods_number']:'';
            $goods_price=!empty($_REQUEST['goods_price'])?$_REQUEST['goods_price']:'';
            $out_code=!empty($_REQUEST['out_code'])?$_REQUEST['out_code']:'';
            $note=!empty($_REQUEST['note'])?$_REQUEST['note']:'';
            $area_id=!empty($_REQUEST['area_id'])?$_REQUEST['area_id']:'';



    

            //插入入货单信息
            $out_info = array(
                'user_name'      => $_SESSION['admin_name'],
                'add_time'   => time(),
                'out_code'        => $out_code,
                'area_id'           => $area_id,
                'note'           => $note,
            );
            do
            {
                $out_info['out_sn'] = get_order_sn();
                if ($db->autoExecute($ecs->table('ware_out_info'), $out_info, 'INSERT', '', 'SILENT'))
                {
                    break;
                }
                else
                {
                    if ($db->errno() != 1062)
                    {
                        die($db->error());
                    }
                }
            }
            while (true); // 防止订单号重复
            $out_id = $db->insert_id();
            //重组商品信息
            foreach ($goods_id as $key => $value) {
                $goods_list[$key]['out_id']=$out_id;
                $goods_list[$key]['goods_id']=$value;
                $goods_list[$key]['goods_attr_id']=$goods_attr_id[$key];
                $goods_list[$key]['goods_name']=$goods_name[$key];
                $goods_list[$key]['goods_attr_name']=$goods_attr_name[$key];
                $goods_list[$key]['goods_sku']=$goods_sku[$key];
                $goods_list[$key]['goods_number']=$goods_number[$key];
                $goods_list[$key]['goods_price']=$goods_price[$key];
            }
            //循环插入出货商品
            foreach ($goods_list as $key => $value) {
                $db->autoExecute($ecs->table('ware_out_goods'), $value, 'INSERT', '', 'SILENT');            
                //减少库存
                change_goods_storage($value['goods_sku'],$area_id,-$value['goods_number']);
            }
            //记录管理员操作
            ware_log($_SESSION['admin_name'],'5',$note,$area_id);
            //完成返回入货单列表
            header("Location: warehouse.php?act=out_list");
    }
    $smarty->display('ware/out_add.htm');
}
//出货单详情
elseif ($_REQUEST['act']=="out_info") {
        /* 检查权限 */
        admin_priv('out_info');
        /* 模板赋值 */
        $smarty->assign('ur_here', "出货单详情");
        $smarty->assign('action_link', array('href' => 'warehouse.php?act=out_list', 'text' => "出货单列表"));

        $out_id=!empty($_REQUEST['out_id'])?$_REQUEST['out_id']:'';
        if(empty($out_id)){
            header("Location: warehouse.php?act=out_list");
        }
        // //出货单信息
        $out_info=out_info($out_id);
        $smarty->assign('out_info',   $out_info['info']);
        $smarty->assign('goods_list',   $out_info['goods']);
        $smarty->display('ware/out_info.htm'); 
}
//删除出货单
elseif ($_REQUEST['act']=='out_delete'){
        $out_id=!empty($_REQUEST['out_id'])?$_REQUEST['out_id']:'';
        //返回商品库存

        //获取出货单信息
        $out_info=out_info($out_id);
        $area_id=$out_info['info']['area_id'];
        //循环插入出货商品
        foreach ($out_info['goods'] as $key => $value) {           
            //反弹出货库存
            change_goods_storage($value['goods_sku'],$area_id,$value['goods_number']);
        }
        //删除出货单信息
        $sql = "DELETE FROM ".$GLOBALS['ecs']->table('ware_out_info')." WHERE out_id=$out_id";
        $db->query($sql);
        //删除出货商品
        $sql = "DELETE FROM ".$GLOBALS['ecs']->table('ware_out_goods')." WHERE out_id=$out_id";
        $db->query($sql);   
        $links[] = array('href' => 'warehouse.php?act=out_list', 'text' => "出货单删除成功点击返回");
        sys_msg("删除成功", 100, $links);
}
//删除进货单
elseif ($_REQUEST['act']=='buy_delete'){
        $buy_id=!empty($_REQUEST['buy_id'])?$_REQUEST['buy_id']:'';
        //返回商品库存
        //获取出货单信息
        $buy_info=buy_info($buy_id);
        $area_id=$buy_info['info']['area_id'];
        //循环插入出货商品
        foreach ($buy_info['goods'] as $key => $value) {           
            //减少进货库存
            change_goods_storage($value['goods_sku'],$area_id,-$value['goods_number']);
        }
        //删除出货单信息
        $sql = "DELETE FROM ".$GLOBALS['ecs']->table('ware_buy_info')." WHERE buy_id=$buy_id";
        $db->query($sql);
        //删除出货商品
        $sql = "DELETE FROM ".$GLOBALS['ecs']->table('ware_buy_goods')." WHERE buy_id=$buy_id";
        $db->query($sql);   
        $links[] = array('href' => 'warehouse.php?act=buy_list', 'text' => "进货单删除成功点击返回");
        sys_msg("删除成功", 100, $links);
}
//进货单统计
elseif ($_REQUEST['act']=='buy_stats') {
            // include_once('./includes/PHPExcel.php');
            // error_reporting(E_ALL);
            // date_default_timezone_set('Europe/London');
            // $objPHPExcel = new PHPExcel();
            // $name="广州最新价格";
            // //获取导出内容
            // $sql =  "SELECT g.goods_id,g.goods_sku,g.goods_name,g.shop_price,g.goods_weight,g.preferential_price2,g.preferential_price5,gs.goods_xprice FROM "
            //         .$GLOBALS['ecs']->table('goods')." AS g ".
            //         " LEFT JOIN ".$GLOBALS['ecs']->table('goods_stock')." AS gs ON g.goods_id = gs.goods_id".
            //         " WHERE g.is_on_sale = 1 AND gs.area_id = 1 ".
            //         " group by g.goods_id ";
            // $goods_list=$db->getAll($sql);
            // //设置表格属性
            // $objPHPExcel->setActiveSheetIndex(0); 
            // $objActSheet = $objPHPExcel->getActiveSheet(); 
            // $objActSheet->getColumnDimension('A')->setWidth(50);
            // // 标题写入
            // $objPHPExcel->setActiveSheetIndex(0)
            // ->setCellValue('A1', 'ID')    
            // ->setCellValue('B1', '商品SKU')
            // ->setCellValue('C1', '商品名称')
            // ->setCellValue('D1', '重量')
            // ->setCellValue('E1', '进货价')
            // ->setCellValue('F1', '销售价格')
            // ->setCellValue('G1', '梯度2')
            // ->setCellValue('H1', '梯度5');
            // //导出数据写入
            // foreach($goods_list as $k => $v){
            //     if(!$v['goods_name']) continue;
            //         $num=$k+2;
            //         $objPHPExcel->setActiveSheetIndex(0)
            //         //Excel的第A列，uid是你查出数组的键值，下面以此类推
            //         ->setCellValue('A'.$num, $v['goods_id'])
            //         ->setCellValue('B'.$num, $v['goods_sku'])
            //         ->setCellValue('C'.$num, $v['goods_name'])
            //         ->setCellValue('D'.$num, $v['goods_weight'])
            //         ->setCellValue('E'.$num, $v['goods_xprice'])
            //         ->setCellValue('F'.$num, $v['shop_price'])
            //         ->setCellValue('G'.$num, $v['preferential_price2'])
            //         ->setCellValue('H'.$num, $v['preferential_price5']);
            // }
            // $objPHPExcel->getActiveSheet()->setTitle('User');
            // $objPHPExcel->setActiveSheetIndex(0);
            // header('Content-Type: application/vnd.ms-excel');
            // header('Content-Disposition: attachment;filename="'.$name.'.xls"');
            // header('Cache-Control: max-age=0');
            // $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            // $objWriter->save('php://output');
                /* 检查权限 */
            admin_priv('buy_info');
            // /* 模板赋值 */
            $smarty->assign('ur_here', "进货统计");
            $smarty->assign('action_link', array('href' => 'warehouse.php?act=buy_list', 'text' => "进货单列表"));
            $smarty->assign('agency_list',  agency_list());//办事处列表
            $buy_stats_list = buy_stats_list();//进货统计列表
            $smarty->assign('buy_list',   $buy_stats_list['buy']);
            $smarty->assign('filter',       $buy_stats_list['filter']);
            $smarty->assign('record_count', $buy_stats_list['record_count']);
            $smarty->assign('page_count',   $buy_stats_list['page_count']);
            $smarty->assign('time_start',    date("Y-m-d",(time()-86400)));
            $smarty->assign('time_end',    date("Y-m-d",time()));
            $smarty->assign('full_page',    1);
            // /* 区域 */
            if(!empty($_REQUEST['area_id'])){
                $sql = "SELECT agency_name FROM ".$GLOBALS['ecs']->table('agency')." WHERE agency_id = $_REQUEST[area_id] ";
                $agency_name=$db->getOne($sql);
            }
            else{
                $agency_name="全部区域";
            }
            $smarty->assign('agency_name',    $agency_name);
            $smarty->display('ware/buy_stats.htm'); 
}
//进货统计分页
elseif ($_REQUEST['act'] == 'buy_stats_query')
{
    $is_delete = empty($_REQUEST['is_delete']) ? 0 : intval($_REQUEST['is_delete']);
    $buy_stats_list = buy_stats_list();//进货单列表
    $smarty->assign('code',         $code);
    $smarty->assign('buy_list',   $buy_stats_list['buy']);
    $smarty->assign('filter',       $buy_stats_list['filter']);
    $smarty->assign('record_count', $buy_stats_list['record_count']);
    $smarty->assign('page_count',   $buy_stats_list['page_count']);
    $smarty->assign('list_type',    $is_delete ? 'trash' : 'goods');
    $smarty->assign('use_storage',  empty($_CFG['use_storage']) ? 0 : 1);
    if(!empty($_REQUEST['area_id'])){
        $sql = "SELECT agency_name FROM ".$GLOBALS['ecs']->table('agency')." WHERE agency_id = $_REQUEST[area_id] ";
        $agency_name=$db->getOne($sql);
    }
    else{
        $agency_name="全部区域";
    }
    $smarty->assign('agency_name',    $agency_name);
    make_json_result($smarty->fetch('ware/buy_stats.htm'), '',
    array('filter' => $buy_stats_list['filter'], 'page_count' => $buy_stats_list['page_count']));
}

//出货统计
elseif ($_REQUEST['act']=='out_stats') {
            /* 检查权限 */
            admin_priv('buy_info');
            // /* 模板赋值 */
            $smarty->assign('ur_here', "出货统计");
            $smarty->assign('action_link', array('href' => 'warehouse.php?act=out_list', 'text' => "出货单列表"));
            $smarty->assign('agency_list',  agency_list());//办事处列表
            $buy_stats_list = out_stats_list();//进货统计列表
            $smarty->assign('buy_list',   $buy_stats_list['buy']);
            $smarty->assign('filter',       $buy_stats_list['filter']);
            $smarty->assign('record_count', $buy_stats_list['record_count']);
            $smarty->assign('page_count',   $buy_stats_list['page_count']);
            $smarty->assign('time_start',    date("Y-m-d",(time()-86400)));
            $smarty->assign('time_end',    date("Y-m-d",time()));
            $smarty->assign('full_page',    1);
            // /* 区域 */
            if(!empty($_REQUEST['area_id'])){
                $sql = "SELECT agency_name FROM ".$GLOBALS['ecs']->table('agency')." WHERE agency_id = $_REQUEST[area_id] ";
                $agency_name=$db->getOne($sql);
            }
            else{
                $agency_name="全部区域";
            }
            $smarty->assign('agency_name',    $agency_name);
            $smarty->display('ware/buy_stats.htm'); 
}
//出库统计分页
elseif ($_REQUEST['act'] == 'out_stats_query')
{
    $is_delete = empty($_REQUEST['is_delete']) ? 0 : intval($_REQUEST['is_delete']);
    $buy_stats_list = out_stats_list();//进货单列表
    $smarty->assign('code',         $code);
    $smarty->assign('buy_list',   $buy_stats_list['buy']);
    $smarty->assign('filter',       $buy_stats_list['filter']);
    $smarty->assign('record_count', $buy_stats_list['record_count']);
    $smarty->assign('page_count',   $buy_stats_list['page_count']);
    $smarty->assign('list_type',    $is_delete ? 'trash' : 'goods');
    $smarty->assign('use_storage',  empty($_CFG['use_storage']) ? 0 : 1);
    if(!empty($_REQUEST['area_id'])){
        $sql = "SELECT agency_name FROM ".$GLOBALS['ecs']->table('agency')." WHERE agency_id = $_REQUEST[area_id] ";
        $agency_name=$db->getOne($sql);
    }
    else{
        $agency_name="全部区域";
    }
    $smarty->assign('agency_name',    $agency_name);
    make_json_result($smarty->fetch('ware/buy_stats.htm'), '',
    array('filter' => $buy_stats_list['filter'], 'page_count' => $buy_stats_list['page_count']));
}










/**
 * 数字转金额大写
 *
 * @access  private
 * @return  Bool
 */
function cny($ns) { 
    static $cnums=array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"), 
        $cnyunits=array("圆 ","角 ","分 "), 
        $grees=array("拾 ","佰 ","仟 ","万 ","拾 ","佰 ","仟 ","亿 "); 
    list($ns1,$ns2)=explode(".",$ns,2); 
    $ns2=array_filter(array($ns2[1],$ns2[0])); 
    $ret=array_merge($ns2,array(implode("",_cny_map_unit(str_split($ns1),$grees)),"")); 
    $ret=implode("",array_reverse(_cny_map_unit($ret,$cnyunits))); 
    return str_replace(array_keys($cnums),$cnums,$ret); 
}
function _cny_map_unit($list,$units) { 
    $ul=count($units); 
    $xs=array(); 
    foreach (array_reverse($list) as $x) { 
        $l=count($xs); 
        if ($x!="0" || !($l%4)) $n=($x=='0'?'':$x).($units[($l-1)%$ul]); 
        else $n=is_numeric($xs[0][0])?$x:''; 
        array_unshift($xs,$n); 
    } 
    return $xs; 
}

?>